package com.whpost.service.dao.wx;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.whpost.service.entity.wx.TWxOrder;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;

public interface TWxOrderDao extends BaseMapper<TWxOrder> {

    //获取已经存在的可用的prepay_id
    @Select("select return_prepay_id from t_wx_order where tname=#{tname} and zblsh=#{zblsh} and send_time_expire > to_char(#{nowDate},'yyyyMMddHH24mmss') and send_total_fee=#{totalFee}")
    String checkPayId(@Param(value = "tname") String tname, @Param(value = "zblsh") String zblsh, @Param(value = "totalFee") String totalFee,@Param(value = "nowDate") Date nowDate);

    //看有没有已经成功付款的，
    @Select("select return_prepay_id from t_wx_order where zblsh=#{zblsh} and tname=#{tname} and send_total_fee=#{totalFee} and wcpay_request='1'")
    String checkPayIdSuc(@Param(value = "tname") String tname, @Param(value = "zblsh") String zblsh, @Param(value = "totalFee") String totalFee);

    //更新t_wx_order  prepay_id, id, return_code, return_code_url
    @Update("update t_wx_order set return_prepay_id=#{prepay_id},return_code=#{return_code},return_code_url=#{return_code_url} where lsh=#{id}")
    int updateWxOrderByRtnJson(@Param(value = "prepay_id") String prepay_id, @Param(value = "id") Long id, @Param(value = "return_code") String return_code, @Param(value = "return_code_url") String return_code_url);

    //删除下单失败的订单
    @Delete("delete from t_wx_order where send_out_trade_no = #{send_out_trade_no} and wcpay_request!='1'")
    int delFailWxOrder(@Param(value = "send_out_trade_no") String send_out_trade_no);

    //selectPrice
    @Select("select act_fee from ${tableName} where id = #{id}")
    String selectPrice(@Param(value = "tableName") String tableName, @Param(value = "id") String id);

    //更新微信订单表，设置为支付成功
    @Update("update t_wx_order set wcpay_request='1',transaction_id=#{transaction_id} where send_out_trade_no = #{send_out_trade_no} and wcpay_request!='1'")
    int updateWxOrderByPaySuc(@Param(value = "send_out_trade_no") String send_out_trade_no, @Param(value = "transaction_id") String transaction_id);

    //更新微信订单表，设置为退款成功
    @Update("update t_wx_order set tkbz='1' where lsh = #{id}")
    int updateWxOrderTkbz(@Param(value = "id") Long id);

    //根据订单号获取信息
    @Select("select * from t_wx_order where send_out_trade_no=#{tradeNo}")
    TWxOrder getTWxOrderByTradeNo(String tradeNo);

    //根据订单号获取信息
    @Select("select * from (select * from t_wx_order where tname='stamp_card_order' and zblsh=#{id} order by lsh desc) where rownum=1")
    TWxOrder getTWxOrderByZblsh(String id);

}